package com.haoshuang.zhouzhoubang.ui.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.FragmentPushBinding
import com.haoshuang.zhouzhoubang.model.bean.TsBean
import com.haoshuang.zhouzhoubang.ui.activity.PushOrder2Activity
import com.haoshuang.zhouzhoubang.ui.adapter.PushAdapter
import com.haoshuang.zhouzhoubang.ui.vm.PushViewModel
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast

/**
 *  
 */
class PushFragment : BaseVMFragment<PushViewModel, FragmentPushBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        act=activity as PushOrder2Activity
        adapter= PushAdapter(list)
        binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        binding.recyclerView.adapter=adapter
        adapter.setEmptyData()
        binding.refreshLayout.setOnRefreshListener {
            p = 1
            getNet()
            binding.refreshLayout.finishRefresh(2000)
            binding.refreshLayout.setEnableLoadMore(true)
        }
        binding.refreshLayout.setOnLoadMoreListener {
            if (list.size < 10) {
                binding.refreshLayout.setEnableLoadMore(false)
            } else {
                p++
                getNet()
            }
            binding.refreshLayout.finishLoadMore(2000)
        }
    }
    var list= arrayListOf<TsBean>()
    lateinit var adapter: PushAdapter
    var act:PushOrder2Activity?=null
    override fun initData() {


    }


    private fun getNet() {
        if (act?.cate_id==0){
            if (act?.filter_type==3){
                vm.getData("type" to act?.type
                    ,"filter_type" to act?.filter_type
                    ,"start_date" to act?.start_date
                    ,"end_date" to act?.end_date
                    ,"page" to p
                    )
            }else{
                vm.getData("type" to act?.type
                    ,"filter_type" to act?.filter_type
                    ,"page" to p
                    )
            }

        }else{
            if (act?.filter_type==3){
                vm.getData("type" to act?.type
                    ,"filter_type" to act?.filter_type
                    ,"start_date" to act?.start_date
                    ,"end_date" to act?.end_date
                    ,"page" to p
                    ,"cate_id" to act?.cate_id
                    )
            }else{
                vm.getData("type" to act?.type
                    ,"filter_type" to act?.filter_type
                    ,"page" to p
                    ,"cate_id" to act?.cate_id
                    )
            }
        }

    }

    override fun lazyLoadData() {
        p = 1
        getNet()
    }

    override fun startObserve() {
         vm.getData.observeState(this) {
                     onLoading { showLoading() }
                     onError { code, msg ->
                         dismissLoading()
                         toast(msg)
                     }
                     onSuccess {
                         dismissLoading()
                         if (it != null) {
                             if (p==1){
                                 list.clear()
                             }
                             list.addAll(it)
                             adapter.notifyDataSetChanged()
                         }
                     }
                 }

    }

    fun setNew() {
        p=1
        getNet()
    }

    fun updata() {
        p=1
        getNet()
    }

}